{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 运行环境\n",
    "\n",
    "- python3.8-32位！32位！32位！\n",
    "- win10\n",
    "- KEPServerEX.6.4\n",
    "\n",
    "### 其他\n",
    "\n",
    "- [项目地址](https://github.com/mkwiatkowski/openopc)\n",
    "- [官方api文档地址](http://openopc.sourceforge.net/api.html)\n",
    "\n",
    "- opc 模拟服务器下载\n",
    "    - https://www.matrikonopc.com/downloads/178/index.aspx\n",
    "\n",
    "    - https://pan.baidu.com/s/1R4bC-NKLEl4s4FqRfhAIwQ \n",
    "    提取码：07yy\n",
    "\n",
    "\n",
    "## 环境以及依赖配置\n",
    "\n",
    "### OpenOPC 配置\n",
    "\n",
    "1. [下载 OpenOPC-1.3.1.win32-py2.7.exe](https://sourceforge.net/projects/openopc/files/) 并安装\n",
    "\n",
    "2. 将 OpenOPC 安装目录下 src 文件夹下的 `OpenOPC.py` （其他3个py文件随意）复制到 python3.8 安装目录下的 `Lib\\site-packages` 目录下，或者你的项目目录下（能import 就行）。\n",
    "\n",
    "3. **使用2to3工具**\n",
    "\n",
    "  安装python回自动安装2to3工具，能够将python2 的代码一键转为python3的，支持批量修改\n",
    "  \n",
    "  - 查看使用帮助，执行：\n",
    "  ```sh\n",
    "  2to3 --help\n",
    "  ```\n",
    "  - 这里我们直接把原来的python2 代码替换掉，从此以后只用python3，执行：\n",
    "  \n",
    "  ```sh\n",
    "  2to3 -w -n OpenOPC.py\n",
    "  ```  \n",
    "   -n 参数表示不备份原代码\n",
    "\n",
    "4. 修改包名\n",
    "\n",
    "  将 OpenOPC.py 中的 Pyro 全部替换为Pyro4  \n",
    "  (升级到 Pyro5的请参考：[兼容性转换](https://pyro5.readthedocs.io/en/latest/intro.html#upgrading-from-pyro4))\n",
    "\n",
    "4. 修改环境变量\n",
    "\n",
    "  环境变量从哪改就不说了吧\n",
    "  >OPC_MODE = open\n",
    "\n",
    "### pip 包依赖\n",
    "\n",
    "```sh\n",
    "pip install Pywin32\n",
    "pip install OpenOPC-Python3x\n",
    "pip install Pyro4   # 此处为 Pyro4（已经有Pyro5）\n",
    "```\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## api 使用\n",
    "\n",
    "参考上一篇 python2 的openOPC用法，注意开始创建客户端的方法不一样了：\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "from openopc import OpenOPC\n",
    "\n",
    "GROUP_NAME = 'test'\n",
    "\n",
    "opc = OpenOPC.client()\n",
    "# 查询可用服务\n",
    "print(opc.servers(opc_host='localhost'))\n",
    "   \n",
    "opc.connect('Kepware.KEPServerEX.V6', 'localhost')\n",
    "print(opc.list('通道 1.设备 1', recursive=True))\n",
    "\n",
    "# 点位列表\n",
    "taglist = [u'通道 1.设备 1.标记 1', u'通道 1.设备 1.标记 2', u'通道 1.设备 1.TAG1']\n",
    "# 创建组对象\n",
    "opc.read(taglist, group=GROUP_NAME)\n",
    "\n",
    "try:\n",
    "    # 循环周期读取\n",
    "    while True:\n",
    "        # 请求组\n",
    "        opc_data = opc.read(group=GROUP_NAME)\n",
    "        # 保存字典\n",
    "        send_values = {}\n",
    "        for item in opc_data:\n",
    "            name, value, quality, time_ = item\n",
    "            if quality == 'Good':\n",
    "                send_values[name] = value\n",
    "            else:\n",
    "                print('Error:  {}'.format(item))\n",
    "        print(send_values)\n",
    "\n",
    "        time.sleep(5)\n",
    "finally:\n",
    "    # 释放资源\n",
    "    opc.remove(opc.groups())\n",
    "    opc.close()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**详细用法请参考 官方 API**"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "236.2px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
